package controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import management.ApplicationManager;
import model.Candidato;
import utilities.Constants;
import utilities.Utils;
import view.AddCandidateView;
import view.ConfigurationView;
import view.GenericView;

public class ConfigurationController implements GenericController {

	private AddCandidateController addCandidateController;
	private StartController parentController;
	private Candidato candidateBeingModified = null;
	private Set<String> candidatosBorrados = new HashSet<String>();
	private Boolean restart = Boolean.FALSE;
	
	public ConfigurationController(StartController controller){
		this.parentController = controller;
		this.addCandidateController = new AddCandidateController(this);
	}
	
	@Override
	public GenericView getView() {
		return ConfigurationView.getInstance();
	}

	public void handleButtonAddCandidate() {
		this.addCandidateController.createView();
		((AddCandidateView)this.addCandidateController.getView()).setController(this.addCandidateController);
		this.getView().hideWindow();
		this.addCandidateController.getView().showWindow();
	}

	@Override
	public GenericController getParentController() {
		return this.parentController;
	}

	public void notityChangeInCandidatesList() {
		((ConfigurationView)this.getView()).updateList();
	}

	public void removeCandidate(String nombreCandidato) {
		ApplicationManager manager = this.parentController.getManager();
		Candidato candidate = manager.getCandidate(nombreCandidato);
		this.candidatosBorrados.add(candidate.getName());
	//	manager.removeCandidate(nombreCandidato);
		this.notityChangeInCandidatesList();
	}

	public void editCandidate(String nombreCandidato) {
		Candidato c = this.parentController.getManager().getCandidate(nombreCandidato);
		this.candidateBeingModified = c;
		this.addCandidateController.createView();
		AddCandidateView view = ((AddCandidateView)this.addCandidateController.getView());
		view.setController(this.addCandidateController);
		view.getTxtNombre().setText(c.getName());
		Icon icon = Utils.setImageToLabel(Constants.defaultPath + c.getId(), Constants.WIDTH, Constants.HEIGHT);
		view.getLblImagen().setIcon(icon);
		view.setPath(Constants.defaultPath + c.getId());
		this.getView().hideWindow();
		view.showWindow();
	}

	public Candidato getCandidateBeingModified() {
		return candidateBeingModified;
	}

	public void setCandidateBeingModified(Candidato candidateBeingModified) {
		this.candidateBeingModified = candidateBeingModified;
	}

	public void notifyChangeInCandidate(String nombre, JLabel lblImagen,
			String path) {
		Integer id = this.candidateBeingModified.getId();
		String name = this.candidateBeingModified.getName();
		Integer cantVotos = this.candidateBeingModified.getCantVotos();
		Utils.moveFile(path, this.candidateBeingModified.getId());
		Candidato c = new Candidato(nombre, id);
		c.setCantVotos(cantVotos);
		ApplicationManager manager = this.parentController.getManager();
		manager.removeCandidate(name);
		if (manager.addNewCandidate(c)){
			((ConfigurationView)this.getView()).updateList();
		} else {
			manager.addNewCandidate(candidateBeingModified);
			JDialog dialogo = new JDialog(); 
			JOptionPane.showMessageDialog(dialogo,"Ya existe un usuario con ese nombre", "Notificacion",
		                    JOptionPane.INFORMATION_MESSAGE);
		}
		this.candidateBeingModified = null;
	}

	public void handleButtonGuardarCambios() {
		for (String s : this.candidatosBorrados) {
			this.parentController.getManager().removeCandidate(s);
		}
		if (this.restart){
			this.parentController.getManager().restart();
		}
		this.parentController.getManager().guardar();
		this.getView().hideWindow();
		this.parentController.getView().showWindow();
		this.candidatosBorrados.clear();
	}

	public void handleButtonCancelar() {
		this.getView().hideWindow();
		this.parentController.getView().showWindow();
		this.candidatosBorrados.clear();
	}

	public ArrayList<Candidato> getCurrentCandidates() {
		List<Candidato> candidates = this.parentController.getManager().getCandidates();
		ArrayList<Candidato> currentCandidates = new ArrayList<Candidato>();
		for (Candidato candidato : candidates) {
			if (!this.candidatosBorrados.contains(candidato.getName())){
				currentCandidates.add(candidato);
			}
		}
		return currentCandidates;
	}

	public void handleButtonReiniciarVotacion() {
		this.restart = Boolean.TRUE;
		JDialog dialogo = new JDialog(); 
		JOptionPane.showMessageDialog(dialogo,"Votación reiniciada, presione guardar cambios para confirmar", "Notificacion",
	                    JOptionPane.INFORMATION_MESSAGE);
	}

}
